package UI;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import DBTasks.AdoptionsSummary;
import DBTasks.CurrentYearPetAdoptions;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


@SuppressWarnings("serial")
public class YearlyPanel extends JPanel implements ActionListener{

	private JButton returnButton;
	private DefaultTableModel model;
	private JTable table;
	private JTableHeader headers;
	private MainFrame frame;

	public YearlyPanel(MainFrame mainFrame) {
		
		this.setSize(950, 600);
		this.frame = mainFrame;
		
		JLabel logo = new JLabel("Atlanta Pet Adoption Consortium");
		logo.setFont(new Font("Tahoma", Font.BOLD, 30));
		
		String shelterName = (String) frame.getLogin().getShelterList().getSelectedItem();
		JLabel shelterLabel = new JLabel("Current Year Pet Adoptions Summary for " + shelterName);
		shelterLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		
		String calendarDate = dateFormat.format(date);
		JLabel dateLabel = new JLabel("Date: " + calendarDate);
		dateLabel.setFont(new Font("Tahoma", Font.PLAIN, 18));
		
		returnButton = new JButton("Return");
		returnButton.addActionListener(this);
		
		String[] columnNames = {"Month", "Dogs Adopted", "New Dogs", "Dogs in Shelter", "Dogs in Foster Homes", 
										 "Cats Adopted", "New Cats", "Cats in Shelter", "Cats in Foster Homes"};
		List<AdoptionsSummary> adoptions = CurrentYearPetAdoptions.getReport(shelterName);
		@SuppressWarnings("deprecation")
		Object[][] data = new Object[date.getMonth() + 2][9];
		
		int dogAdoptionTotal = 0;
		int newDogTotal = 0;
		int catAdoptionTotal = 0;
		int newCatTotal = 0;
		
		for(int i = 0; i < data.length - 1; i++) {
			data[i][0] = frame.getMonth(i);
			data[i][1] = adoptions.get(i).getAdoptedDogs();
			data[i][2] = adoptions.get(i).getNewDogs();
			data[i][3] = adoptions.get(i).getShelteredDogs();
			data[i][4] = adoptions.get(i).getFosteredDogs();
			data[i][5] = adoptions.get(i).getAdoptedCats();
			data[i][6] = adoptions.get(i).getNewCats();
			data[i][7] = adoptions.get(i).getShelteredCats();
			data[i][8] = adoptions.get(i).getFosteredCats();
			
			dogAdoptionTotal += adoptions.get(i).getAdoptedDogs();
			catAdoptionTotal += adoptions.get(i).getAdoptedCats();
			newDogTotal += adoptions.get(i).getNewDogs();
			newCatTotal += adoptions.get(i).getNewCats();
		}
		
		data[data.length - 1][0] = "Year to Date";
		data[data.length - 1][1] = dogAdoptionTotal;
		data[data.length - 1][2] = newDogTotal;
		data[data.length - 1][5] = catAdoptionTotal;
		data[data.length - 1][6] = newCatTotal;
		
		model = new DefaultTableModel(data, columnNames);
		table = new JTable(model);
		table.setGridColor(Color.black);
		headers = table.getTableHeader();
		table.setBorder(BorderFactory.createLineBorder(Color.black));
		table.setRowHeight(30);
		headers.setBorder(BorderFactory.createLineBorder(Color.black));
		
        GroupLayout gl = new GroupLayout(this);
		
		gl.setHorizontalGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
				.addComponent(logo)
				.addGroup(gl.createSequentialGroup()
						.addComponent(shelterLabel)
						.addGap(425)
						.addComponent(dateLabel))
				.addComponent(headers)
				.addComponent(table)
				.addGroup(gl.createSequentialGroup()
						.addGap(850)
						.addComponent(returnButton)));
		
	    gl.setVerticalGroup(gl.createSequentialGroup()
	    		.addGap(15)
			    .addComponent(logo)
			    .addGap(40)
			    .addGroup(gl.createParallelGroup(GroupLayout.Alignment.CENTER)
						.addComponent(shelterLabel)
						.addComponent(dateLabel))
			    .addGap(20)
			    .addComponent(headers)
			    .addComponent(table)
			    .addGap(20)
			    .addComponent(returnButton)
			    .addGap(40)
	    );
		
		gl.setAutoCreateContainerGaps(true);
		
		this.setLayout(gl);
		
	}
	
	public void paintComponent(Graphics g) {
        super.paintComponents(g);
        g.setColor(Color.blue);
        g.fillRect(0, 0, this.getWidth(), 70);
    }

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(returnButton)) {
			frame.setPanel(frame.getHomepage());
		}
	}

}
